/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details:
 * http://www.gnu.org/copyleft/lesser.html#TOC3
 */
package com.simplemonkey.widgets;

import com.jme.image.Texture;
import com.jme.scene.Node;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.simplemonkey.IResizeListener;
import com.simplemonkey.Widget;
import com.simplemonkey.util.Dimension;

public class TextureWidget extends Widget {
	
	private Node spatial;
	protected Quad background;
	private TextureState backgroundTexture;
	private Dimension minContentSize = new Dimension(1,1);
	

	public TextureState getBackgroundTexture() {
		return backgroundTexture;
	}

	public TextureWidget()
	{
		this("Texture Widget");
	}
	
	public void setMinContentSize(Dimension minContentSize) {
		this.minContentSize = minContentSize;
		this.updateMinSize();
	}

	
	public Dimension getMinContentSize() {
		return minContentSize;
		}

	public TextureWidget(String name) {
		super(name);
		background = new Quad("TextureWidget",1,1);
		
		background.getLocalTranslation().set(0.5f,0.5f,0);
		
		backgroundTexture = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
		
		background.copyTextureCoordinates(0, 0, 1);
		
		background.setRenderState(backgroundTexture);
		background.updateRenderState();
		
		spatial = new Node();
		spatial.attachChild(background);
		spatial.updateWorldVectors();
		spatial.updateRenderState();
		
		super.addResizeListener(new IResizeListener()
		{

			
			public void resize(Dimension newSize) {
				
				getSpatial().getLocalScale().set(getSpacingAppearance().getContentWidth()+ getSpacingAppearance().getPadding().getLeftPlusRight(),getSpacingAppearance().getContentHeight()+ getSpacingAppearance().getPadding().getBottomPlusTop(),1f);
				getSpatial().updateGeometricState(0, true);
			}
			
		});
	}
	public void removeTexture(int unit)
	{
		backgroundTexture.removeTexture(unit);
		background.updateRenderState();
	}
	public void setTexture(Texture texture, int unit)
	{
		backgroundTexture.setTexture(texture,unit);
		background.updateRenderState();
	}
	
	public Texture getTexture(int unit)
	{
		return backgroundTexture.getTexture(unit);
	}
	
	public void setTexture(Texture texture)
	{
		this.setTexture(texture,0);
	}
	


	
	public Node getSpatial() {
	
		return spatial;
	}

}
